package com.fen.demo002;

import com.fen.demo001.MessageProvider;
import com.fen.demo001.MessageRenderer;

import java.util.Properties;

/**
 * @author : lrf
 * @description :
 * @tip :
 * @date : 2019/3/21
 */
public class MessageSupportFactory {

    private static MessageSupportFactory instance;

    private Properties props;
    private MessageRenderer renderer;
    private MessageProvider provider;

    private MessageSupportFactory() {
        this.props = new Properties();

        try {
            // 获取 classpath 路径下的文件
//            props.load(this.getClass().getResourceAsStream("/demo002/msf.properties"));
            props.load(MessageProvider.class.getResourceAsStream("/demo002/msf.properties"));
            String rendererClass = props.getProperty("renderer.class");
            String propertyClass = props.getProperty("provider.class");

            renderer = (MessageRenderer) Class.forName(rendererClass).newInstance();
            provider = (MessageProvider) Class.forName(propertyClass).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static {
        instance = new MessageSupportFactory();
    }

    public static MessageSupportFactory getInstance() {
        return instance;
    }

    public MessageRenderer getRenderer() {
        return renderer;
    }

    public MessageProvider getProvider() {
        return provider;
    }
}
